---
title: "Descriptive Figures"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  pdf_document:
    toc: true
    toc_depth: 4
params:
  stargazer_type: "latex"
---

```{r, echo=FALSE}
knitr::opts_chunk$set(echo=FALSE,
                      message=FALSE, 
                      warning=FALSE)
```

```{r}
filepath_replication_data <- '[ENTER DIRECTORY HERE]'
```

```{r lib, message=FALSE}
library(scales)
library(tidyverse)
library(stargazer)
library(lfe)
library(knitr)
library(haven)
library(broom)
library(tidycat)
library(knitr)
library(kableExtra)
library(gridExtra)
library(ggpubr)
```

```{r source_scripts}
source("helper-objects.R")
source("helper-functions.R")
```

```{r LOAD_SURVEY_DATA_dat, message=FALSE}
dat <- read_dta(file = file.path(filepath_replication_data,
                                 "final_replication_survey_dataset.dta")) %>% 
  filter(wave %in% 1:4) %>% 
  rename(Q147_would_get_covid_vaccine_yes_binary   = Q147_would_get_covid_vaccine_yb,
         Q146_flu_vaccine_2020_yes_binary          = Q146_flu_vaccine_2020_yes_binar,
         zip_hh_inc_2018_median_range_middle       = zip_hh_inc_2018_median_range_mi, 
         zip_educ_2018_high_school_or_less         = zip_educ_2018_high_school_or_le, 
         demo_household_income_range_middle        = demo_household_income_range_mid,
         demo_race_ethnicity_binned_hispanic       = demo_race_ethnicity_binned_hisp,
         demo_household_income_top_quartile_binary = demo_household_income_top_quart,
         zip_hh_inc_2018_prop_top_quartile         = zip_hh_inc_2018_prop_top_quarti,
         Q10x_plan_tried_vaccinated_binary         = Q10x_plan_tried_vaccinated_bina,
         deaths_county_pc_1k_quantile_wave_start   = deaths_county_pc_1k_quant_w_st       
          ) %>% 
  mutate_if(is.labelled, as_factor) %>% 
  mutate(party = ifelse(party == "", NA, party))
```

```{r LOAD_VALUES_FIGURE_S1_PANEL_1}
df_figure_a2.1_treatment_variation_zipcode_zip_measures_ALL_VOTERS <- read_dta( 
  file = file.path(filepath_replication_data,
                   "figures_s1_panel_1_aggregated_geography_data.dta")
) %>% 
  rename(prop.r_zip_bucket5 = prop_r_zip_bucket5) %>% 
  mutate_if(is.labelled, as_factor)
```


```{r dat_long_dvs}
dat_long_dvs <- dat %>% 
  select(
    weights,
    wave,
    # DVs
    Q31_mask_wearing_always_binary,
    Q147_would_get_covid_vaccine_yes_binary,
    Q146_flu_vaccine_2020_yes_binary,
    Q145_flu_vaccine_last_binary,
    # IVs
    party_with_independents,
         Q7_health_dx_three_bins,
         deaths_county_pc_1k_quantile_wave_start
  ) %>% 
  gather(key = dv_name, value = dv_value, 
         Q31_mask_wearing_always_binary,
         Q147_would_get_covid_vaccine_yes_binary,
         Q146_flu_vaccine_2020_yes_binary,
         Q145_flu_vaccine_last_binary
         ) %>% 
    mutate(dv_name_original = dv_name,
         dv_name          = str_remove(dv_name, "^Q[0-9]*_"),
         dv_name          = str_remove(dv_name, "_binary$"),
         dv_name          = str_remove(dv_name, "^activity_")) 
```

```{r dat_long}
dat_long <- dat_long_dvs %>% 
  # Independent Variables
  gather(key = iv_name, value = iv_value,
         party_with_independents,
         Q7_health_dx_three_bins,
         deaths_county_pc_1k_quantile_wave_start) %>% 
  # Clear out Missing Values
  filter(!is.na(dv_value),
         !is.na(iv_value),
         !is.na(weights)) %>% 
  mutate(iv_name = recode(iv_name, 
                          "party_with_independents" = "Party ID",
                          "Q7_health_dx_three_bins" = "Major Health Diagnoses",
                          "deaths_county_pc_1k_quantile_wave_start" = "County COVID Severity"
                          )) 
```

# Figure 1: Trends in Dependent Variables

```{r df_trend_figure1}
df_trend_figure1_overall <- dat_long_dvs %>%  
  group_by(wave, dv_name) %>% 
  summarise(dv_value_mean = weighted.mean(x = dv_value,
                                          w = weights, 
                                          na.rm = TRUE)) %>% 
  ungroup() %>% 
  mutate(iv_value = "Overall",
         iv_name = "Overall") %>% 
  drop_na()

df_trend_figure1_party <- dat_long %>% 
  filter(iv_name %in% c("Party ID", 
                        "Major Health Diagnoses", 
                        "County COVID Severity")) %>% 
  group_by(wave, dv_name, iv_name, iv_value) %>% 
  summarise(dv_value_mean = weighted.mean(x = dv_value,
                                          w = weights)) %>% 
  ungroup() %>% 
  drop_na()

df_trend_figure1 <- bind_rows(df_trend_figure1_overall,
                              df_trend_figure1_party) %>% 
  left_join(tribble(
      ~dv_name, ~dv_name_long,
      "mask_wearing_always",   "Always wear mask",
      "would_get_covid_vaccine_yes", "COVID-19 vaccine",
      "flu_vaccine_last",     "Flu vaccine (2019 season)",
      "flu_vaccine_2020_yes", "Flu vaccine (2020 season)"
    ),
  by = "dv_name"
  ) %>% 
  rename(dv_name_original = dv_name,
         dv_name = dv_name_long) %>% 
  mutate(wave_date = as.Date(
    recode(wave, 
           `1` = "2020-05-11",
           `2` = "2020-07-09",
           `3` = "2020-10-01",
           `4` = "2020-12-04"
  ))) %>% 
  mutate(dv_name_original = fct_relevel(
    dv_name_original,
    "mask_wearing_always",
    "would_get_covid_vaccine_yes",
    "flu_vaccine_2020_yes",
    "flu_vaccine_last",
  ))
```

```{r blank_data}
blank_data <- tibble(
  wave_date = as.Date(c("2020-10-01", "2020-12-01")),
  dv_value_mean = 0.5
) %>% 
  crossing(
    dv_name_original = c("mask_wearing_always", "would_get_covid_vaccine_yes", "flu_vaccine_2020_yes")
  ) %>% 
  mutate(dv_name_original = fct_relevel(dv_name_original,
                                        "mask_wearing_always", "would_get_covid_vaccine_yes", "flu_vaccine_2020_yes")) %>% 
  mutate(iv_value = "[MANUALL ENTER iv_value]")
```


```{r g_trend_figure1_party, fig.width=10, fig.width=4}
g_trend_figure1_party <- df_trend_figure1 %>% 
  filter(iv_name %in% c("Party ID")) %>%
  filter(dv_name_original %in% c(
    "mask_wearing_always",
    "would_get_covid_vaccine_yes",
    "flu_vaccine_2020_yes"
  )) %>%
  mutate(temp_col = "") %>% 
  mutate(iv_value = fct_relevel(iv_value,
                                # "Overall",
                                "Democrat",
                               "Independent", 
                               "Republican")) %>% 
  filter(iv_value != "") %>% 
  # Plot
  ggplot(aes(x = wave_date, 
             y = dv_value_mean, 
             color = iv_value,
             group = iv_value)) +
  geom_blank(data = blank_data %>% 
               mutate(iv_value = "Democrat")) +
  geom_line() +
  geom_point() +
  # Theme and structure
  facet_grid(temp_col ~ dv_name_original, scales = "free_x",
             space = "free_x"  ) +
  theme_bw() +
  theme(legend.direction = "vertical",
        legend.position = "bottom",
        strip.background = element_blank(), 
        strip.text.y = element_blank(),
        panel.spacing = unit(1., "lines")) +
  # Scales and Labels
  scale_color_manual(values = colors_multiple_variables[c("Democrat", "Independent", "Republican")],
                     name = NULL) +
  guides(color = guide_legend(ncol = 1,
                              bycol = TRUE)) +
  expand_limits(y = c(0, 1)) +
  scale_x_date(
    breaks = seq(as.Date("2020-01-01"), as.Date("2021-01-01"), "1 months"), 
    date_labels = "%b", 
    expand = expand_scale(mult = 0.1),
    position = "top",
  ) +
  scale_y_continuous(breaks = seq(0, 1, 0.2),
                     labels = scales::percent) +
  labs(x = NULL, y = NULL) 
```

```{r g_trend_figure1_dx}
g_trend_figure1_dx <- df_trend_figure1 %>% 
  filter(iv_name %in% c("Major Health Diagnoses")) %>%
  filter(dv_name_original %in% c(
    "mask_wearing_always",
    "would_get_covid_vaccine_yes",
    "flu_vaccine_2020_yes"
  )) %>%
  mutate(temp_col = "") %>% 
  mutate(iv_value = fct_relevel(iv_value, "Overall", 
                                "No Diagnoses",
                                "1 or 2 Diagnoses",
                                "3 or more Diagnoses")) %>% 
  filter(iv_value != "1 or 2 Diagnoses") %>%
  mutate(iv_value = fct_relevel(
    iv_value,
    "No Diagnoses",
    "3 or more Diagnoses"
  )) %>% 
  mutate(iv_value = fct_recode(
    iv_value,
    "3 or more\nDiagnoses" = "3 or more Diagnoses"
  )) %>% 
  # Plot
  ggplot(aes(x = wave_date, 
             y = dv_value_mean, 
             color = iv_value,
             group = iv_value)) +
  geom_blank(data = blank_data %>% 
               mutate(iv_value = "No Diagnoses")) +
  geom_line() +
  geom_point() +
  # Theme and structure
  facet_grid(temp_col ~ dv_name_original, scales = "free_x",
             space = "free_x"  ) +
  theme_bw() +
  theme(legend.direction = "vertical",
        legend.position = "bottom",
        strip.background = element_blank(),
        panel.spacing = unit(1., "lines")) +
  # Scales and Labels
  scale_color_manual(
    values = set_names(
      x = colors_multiple_variables, 
      nm = recode(names(colors_multiple_variables),
                  "3 or more Diagnoses" = "3 or more\nDiagnoses")
    ),
    breaks = c("No Diagnoses", "3 or more\nDiagnoses"),
    # No!!!! Causes an error!!!
    # labels = c("1 or 2\nDiagnoses", 
    #            "3 or more\nDiagnoses"),
    name = NULL
  ) +
  guides(color = guide_legend(ncol = 1,
                              bycol = TRUE)) +
  expand_limits(y = c(0, 1)) +
  scale_x_date(
    breaks = seq(as.Date("2020-01-01"), as.Date("2021-01-01"), "1 months"), 
    date_labels = "%b", 
    expand = expand_scale(mult = 0.1), 
    position = "bottom",
  ) +
  scale_y_continuous(breaks = seq(0, 1, 0.2),
                     labels = scales::percent) +
  labs(x = NULL, y = NULL) 

g_trend_figure1_dx
```

```{r df_trend_figure1_covid_deaths_ready}
df_trend_figure1_covid_deaths_init <- df_trend_figure1 %>% 
  filter(iv_name %in% c("County COVID Severity")) %>%
  filter(dv_name_original %in% c(
    "mask_wearing_always",
    "would_get_covid_vaccine_yes",
    "flu_vaccine_2020_yes"
  )) %>%
  mutate(temp_col = "") %>% 
  mutate(iv_value = fct_relevel(iv_value, 
                                "Overall", 
                                "1st Quantile",
                                "2nd Quantile",
                                "3rd Quantile",
                                "4th Quantile")) %>% 
  # Change "Quantile" to "Quartile"
  mutate(iv_value = factor(str_replace(as.character(iv_value), "Quantile", "Quartile"),
                           str_replace(levels(iv_value), "Quantile", "Quartile")))

df_trend_figure1_covid_deaths_subset <- df_trend_figure1_covid_deaths_init %>% 
  filter(wave == 1) %>% 
  filter(iv_value %in% c("1st Quartile", "2nd Quartile"))

df_trend_figure1_covid_deaths_ready <- 
  bind_rows(
    df_trend_figure1_covid_deaths_init,
    mutate(
      df_trend_figure1_covid_deaths_subset,
      iv_value = fct_recode(iv_value,
                            "2nd Quartile" = "1st Quartile")
    )
  )
```

```{r g_trend_figure1_covid_deaths}
g_trend_figure1_covid_deaths <- df_trend_figure1_covid_deaths_ready %>% 
  mutate(iv_value = fct_drop(iv_value)) %>% 
  # Plot
  ggplot(aes(x = wave_date, 
             y = dv_value_mean, 
             color = iv_value,
             group = iv_value,
             fill = iv_value)) +
  geom_blank(data = blank_data %>% 
               mutate(iv_value = "1st Quartile")) +
  geom_line() +
  geom_point() +
  # Theme and structure
  facet_grid(temp_col ~ dv_name_original, scales = "free_x",
             space = "free_x"  ) +
  theme_bw() +
  theme(legend.direction = "vertical",
        legend.position = "bottom",
        strip.background = element_blank(),
        strip.text.y = element_blank(),
        panel.spacing = unit(1., "lines")) +
  # Scales and Labels
  scale_color_manual(values = colors_multiple_variables[sort(as.character(unique(df_trend_figure1_covid_deaths_ready$iv_value)))],
                     name = NULL) +
  scale_fill_manual(values = colors_multiple_variables[sort(as.character(unique(df_trend_figure1_covid_deaths_ready$iv_value)))],
                    name = NULL) +
  guides(color = guide_legend(ncol = 1,
                              bycol = TRUE)) +
  expand_limits(y = c(0, 1)) +
  scale_x_date(
    breaks = seq(as.Date("2020-01-01"), as.Date("2021-01-01"), "1 months"), 
    date_labels = "%b", # '%y
    expand = expand_scale(mult = 0.1),
  ) +
  scale_y_continuous(breaks = seq(0, 1, 0.2),
                     labels = scales::percent) +
  labs(x = NULL, y = NULL) 
```

```{r g_trend_figure1_combined, fig.width=8, fig.height=4.5}
g_trend_figure1_combined <- grid.arrange(
  # Column Titles 
  text_grob("Always worn a mask outdoors\nin the last two weeks",
            hjust = 0.5,
            size = 10),
  # text_grob("COVID \nVaccine",
  #           hjust = 1,
  #           size = 10),
  text_grob("COVID \nVaccine Intention",
            hjust = 0.5,
            size = 10),
  text_grob("2020\nFlu Vaccine",
            hjust = 0.5,
            size = 10),
  text_grob("",
            hjust = 0.5,
            size = 10),
  # Party
  g_trend_figure1_party +
    labs(y = "Political\nParty") +
    theme(legend.position = "none",
          strip.text.x = element_blank(),
          plot.title = element_text(hjust = 0.5,
                                    size = 12)),
  # COVID Deaths
  g_trend_figure1_covid_deaths + 
    labs(y = "Cumulative County\nCOVID-19 Deaths") +
    theme(legend.position = "none",
          strip.text.x = element_blank(),
          plot.title = element_text(hjust = 0.5,
                                    size = 12),
          strip.text.y = element_blank(),   
          axis.text.x = element_blank(),    
          axis.ticks.x = element_blank() ),
  # Diagnosis
  g_trend_figure1_dx + 
    labs(y = "Health\nDiagnoses") +
    theme(legend.position = "none",
          plot.title = element_text(hjust = 0.5,
                                    size = 12),
        strip.text.x = element_blank(), 
        strip.text.y = element_blank()),
  # Legends
  g_legend(g_trend_figure1_party),
  g_legend(g_trend_figure1_covid_deaths),
  g_legend(g_trend_figure1_dx),
  # Controls
  # widths = c(9.25, 1.25, 2.5, 3),
  widths = c(8.5, 2.0, 2.5, 3),
  heights = c(0.5, rep(3, 3)),
  layout_matrix = rbind(c(1:4),
                        c(rep(5, 3), 8),
                        c(rep(6, 3), 9),
                        c(rep(7, 3), 10))
)
```

```{r}
ggsave(
  filename = "Figures/Figure 1.pdf" ,
  plot = g_trend_figure1_combined,
  width = 18,
  height = 22,
  units = "cm",
  dpi = 300
)
```

# Figure 2 + Figure A2.2: Descriptive Share GOP x Party Figures

```{r}
df_plot_prop_r_x_masks_vaccines_y_overall <- dat %>% 
  select(weights, 
         wave, 
         party, prop_r,
         Q31_mask_wearing_always_binary, 
         Q147_would_get_covid_vaccine_yes_binary,
         Q146_flu_vaccine_2020_yes_binary,
         Q145_flu_vaccine_last_binary
         ) %>% 
  # Make long
  pivot_longer(cols = c("Q31_mask_wearing_always_binary", 
                        "Q147_would_get_covid_vaccine_yes_binary",
                        "Q146_flu_vaccine_2020_yes_binary",
                        "Q145_flu_vaccine_last_binary"),
               names_to = "dv_name", values_to = "dv_value") %>% 
  # Remove Independents + missing values in DVs
  drop_na(party, dv_value) %>% 
  mutate(demo_race_ethnicity_binned_hispanic = "Overall")

df_plot_prop_r_x_masks_vaccines_y_race <- dat %>% 
  select(weights,
         wave, 
         party, 
         demo_race_ethnicity_binned_hispanic,
         prop_r,
         Q31_mask_wearing_always_binary, 
         Q147_would_get_covid_vaccine_yes_binary,
         Q146_flu_vaccine_2020_yes_binary,
         Q145_flu_vaccine_last_binary
         ) %>% 
  mutate(demo_race_ethnicity_binned_hispanic = as_factor(demo_race_ethnicity_binned_hispanic)) %>% 
  # Make long
  pivot_longer(cols = c("Q31_mask_wearing_always_binary", 
                        "Q147_would_get_covid_vaccine_yes_binary",
                        "Q146_flu_vaccine_2020_yes_binary",
                        "Q145_flu_vaccine_last_binary"),
               names_to = "dv_name", values_to = "dv_value") %>% 
  # Remove Independents + missing values in DVs
  drop_na(party, dv_value) 

df_plot_prop_r_x_masks_vaccines_y <- bind_rows(
  df_plot_prop_r_x_masks_vaccines_y_overall,
  df_plot_prop_r_x_masks_vaccines_y_race
) %>% 
  mutate(demo_race_ethnicity_binned_hispanic = fct_relevel(
    demo_race_ethnicity_binned_hispanic,
   "Overall",  "White", "Black, or African American", "Hispanic", "Asian or Pacific Islander"
  )) %>% 
  left_join(tribble(
      ~dv_name, ~dv_name_long,
      "Q31_mask_wearing_always_binary",   "Always wear mask",
      "Q147_would_get_covid_vaccine_yes_binary", "COVID-19 vaccine",
      "Q145_flu_vaccine_last_binary",     "Flu vaccine (2019 season)",
      "Q146_flu_vaccine_2020_yes_binary", "Flu vaccine (2020 season)"
    ),
  by = "dv_name"
  ) %>% 
  rename(dv_name_original = dv_name,
         dv_name = dv_name_long) %>% 
  mutate(dv_name = fct_relevel(
    dv_name,
    "Always wear mask",
    "COVID-19 vaccine",
    "Flu vaccine (2019 season)",
    "Flu vaccine (2020 season)"
  )) %>% 
  mutate(dv_name_wrapped = factor(str_replace_all(dv_name, " ", "\n"),
                                  levels = str_replace_all(levels(dv_name), " ", "\n")))
```

## Figure 2

```{r g_figure1_dvs_by_prop_overall, fig.width=5, fig.height=3}
g_figure2_dvs_by_prop_overall <- df_plot_prop_r_x_masks_vaccines_y %>%
 filter(dv_name %in% c(
    "Always wear mask",
    "COVID-19 vaccine",
    "Flu vaccine (2020 season)"
  )) %>% 
  filter(demo_race_ethnicity_binned_hispanic %in% c(
    "Overall"
  )) %>% 
  # Plot
  ggplot(data = .,
         aes(x= prop_r, color = party,fill=party, y = dv_value))+
  geom_smooth(aes(weight=weights),
              method = "loess") + 
  # Theme and Structure
  theme_minimal() +
  theme(legend.position = c(0.75, 0.25),
        strip.placement = "outside", 
        legend.direction = "vertical") +
  facet_wrap( ~ dv_name, nrow = 2, scales = "free_x") +
  # Labels and Scales
  labs(y = "Percent",
       x = '% GOP in ZIP Code') +
  scale_y_continuous(labels = percent) +
  scale_x_continuous(labels = percent, expand = expand_scale(mult = 0.1)) +
  expand_limits(y = c(0, 1)) +
  scale_color_manual(values = colors, name = NULL) +
  scale_fill_manual(values = colors, name = NULL)
  
g_figure2_dvs_by_prop_overall
```

```{r}
ggsave(filename = "Figures/Figure 2.pdf",
       plot = g_figure2_dvs_by_prop_overall,
       width = 11,
       height = 11, 
       units = "cm",
       dpi = 300)
```

\pagebreak

## Figure A2.2 (Appendix)

```{r g_figure_a2.2_dvs_by_prop_r_race, fig.width=8, fig.height=4}
g_figure_a2.2_dvs_by_prop_r_race_white_black_hispanic_aapi <- df_plot_prop_r_x_masks_vaccines_y %>%
  filter(dv_name %in% c(
    "Always wear mask",
    "COVID-19 vaccine"
  )) %>% 
  filter(demo_race_ethnicity_binned_hispanic %in% c(
    "White",  
    "Black, or African American",
    "Hispanic",                  
    "Asian or Pacific Islander" 
  )) %>% 
  mutate(demo_race_ethnicity_binned_hispanic = fct_recode(
    demo_race_ethnicity_binned_hispanic,
    "Black, or\nAfrican American" = "Black, or African American",
    "Asian or\nPacific Islander" = "Asian or Pacific Islander" 
  )) %>% 
  # Plot
  ggplot(data = .,
         aes(x= prop_r, color = party,fill=party, y = dv_value))+
  geom_smooth(aes(weight=weights),
              method = "loess") + 
  # Theme and Structure
  theme_minimal() +
  theme(legend.position = "bottom",
              strip.placement = "outside") +
  facet_grid(dv_name ~ demo_race_ethnicity_binned_hispanic,
             switch = "y") +
  # Labels and Scales
  labs(y = "Percent",
       x = '% GOP in ZIP Code') +
  scale_y_continuous(labels = percent) +
  scale_x_continuous(labels = percent, expand = expand_scale(mult = 0.1)) +
  expand_limits(y = c(0, 1)) +
  scale_color_manual(values = colors, name = NULL) +
  scale_fill_manual(values = colors, name = NULL)
  
g_figure_a2.2_dvs_by_prop_r_race_white_black_hispanic_aapi
```

```{r}
ggsave(filename = "Figures/Figure A2.2.pdf",
       plot = g_figure_a2.2_dvs_by_prop_r_race_white_black_hispanic_aapi,
       width = 9,
       height = 6,
       dpi = 300)
``` 

\pagebreak


# Figure A2.1: Variation in Treatment

```{r df_figure_a2.1_treatment_variation_survey_measures_party}
df_figure_a2.1_treatment_variation_survey_measures_party <- dat %>% 
  select(party, 
         prop_r, weights) %>% 
  filter(party %in% c("Democrat", "Republican")) %>% 
  # Make long
  pivot_longer(names_to  = "variable",
               values_to = "value",
               cols = c(# "Exposure_to_republicans", 
                        "prop_r")) %>% 
  # Remove missing values
  drop_na(party, variable, value) %>% 
 # Construct buckets
  mutate(value_bucket5 = cut(value, 
                             breaks = seq(0, 1, 0.1), 
                             include.lowest = TRUE)) %>% 
  # Aggregate: zipcodes + population (D/R)
  group_by(party, variable, value_bucket5) %>% 
  summarise(weighted_sum = sum(weights)) %>% 
  ungroup() %>% 
  # Calculate Percentages
  group_by(party, variable) %>% 
  mutate(weighted_percent = round(weighted_sum / sum(weighted_sum) * 100, 1)) %>% 
  ungroup() %>% 
  # Prep table (make long, remove columns)
  select(party, variable, value_bucket5, weighted_percent) %>%
  pivot_longer(names_to  = "weight_measure_name",
               values_to = "weight_measure_value",
               cols      = "weighted_percent") 
```

```{r df_figure_a2.1_treatment_variation_survey_measures_ALL_VOTERS}
# Overall Information
df_figure_a2.1_treatment_variation_survey_measures_overall_ALL_VOTERS <- dat %>% 
  select(party,
         prop_r, weights) %>% 
  # Make long
  pivot_longer(names_to  = "variable",
               values_to = "value",
               cols = c(# "Exposure_to_republicans", 
                        "prop_r")) %>% 
  # Remove missing values
  drop_na(party, variable, value) %>% 
  # Replace party with "Overall" (for both Dem + GOP)
  mutate(party = "overall") %>% 
  # Construct buckets
  mutate(value_bucket5 = cut(value, 
                             breaks = seq(0, 1, 0.1), 
                             include.lowest = TRUE)) %>% 
  # Aggregate: zipcodes + population (D/R)
  group_by(party, variable, value_bucket5) %>% 
  summarise(weighted_sum = sum(weights)) %>% 
  ungroup() %>% 
  # Calculate Percentages
  group_by(party, variable) %>% 
  mutate(weighted_percent = round(weighted_sum / sum(weighted_sum) * 100, 1)) %>% 
  ungroup() %>% 
  # Prep table (make long, remove columns)
  select(party, variable, value_bucket5, weighted_percent) %>%
  # Just one measure for survey respondents unlike zipcode-level data
  pivot_longer(names_to  = "weight_measure_name",
               values_to = "weight_measure_value",
               cols = "weighted_percent") 

df_figure_a2.1_treatment_variation_survey_measures_ALL_VOTERS <- bind_rows(
  df_figure_a2.1_treatment_variation_survey_measures_overall_ALL_VOTERS,
  df_figure_a2.1_treatment_variation_survey_measures_party 
) %>% 
  # We have exposure AND % zip GOP in the same data.frame... so need an extra
  # column to distinguish. Also change variable names around to match code above.
  rename(exposure_zip = variable,
         variable = party)
```

```{r df_perc_gop_hist_ready_ALL_VOTERS, fig.width=12, fig.height=4}
df_perc_gop_hist_ready_ALL_VOTERS <- bind_rows(
  df_figure_a2.1_treatment_variation_zipcode_zip_measures_ALL_VOTERS %>% 
    mutate(`Data Source` = paste0(
      "Nationwide Registered Voters\n(N = ",
       comma_format(3)(180571602),
      ")"
      )), 
  df_figure_a2.1_treatment_variation_survey_measures_ALL_VOTERS %>% 
    filter(exposure_zip == "prop_r") %>% 
    mutate(`Data Source` = paste0(
      "UCLA COVID Health & Politics Study\n(N = ",
      comma_format(3)(nrow(dat)),
      ")"
      )) %>% 
    rename(party = variable,
           prop.r_zip_bucket5 = value_bucket5
           )
)  %>% 
  # Take out the "% of ZIP Codes" Measure
  filter(weight_measure_name != "perc_zipcodes") %>% 
  # Recode party
  mutate(party = fct_relevel(party,
                                "total_d_r", "d", "r", "overall", "Democrat", "Republican")) %>% 
  mutate(weight_measure_name = recode(weight_measure_name,
                                      "perc_party_count" = "% of Registered Dem. & Rep.",
                                      "perc_zipcodes" = "% of Zipcodes",
                                      "weighted_percent" = "% of Dem. & Rep. Respondents"),
         party = recode(party, 
                        "n" = "Overall (D + I + R)",
                        "d" = "Democrats",
                        "r" = "Republicans",
                        "Democrat" = "Democrats",
                        "Republican" = "Republicans",
                        "overall" = "Overall (D + I + R)"
                        )
         ) %>% 
  # Recode bucket names
  mutate(prop.r_zip_bucket5_start = str_extract(
    prop.r_zip_bucket5,
    "(?<=(\\[|\\())(0|1)(\\.[0-9]){0,1}"
  )) %>% 
  mutate(prop.r_zip_bucket5_end = str_extract(
    prop.r_zip_bucket5,
    "(?<=(\\,))(0|1)(\\.[0-9]){0,1}(?=\\])"
  )) %>% 
  mutate(prop.r_zip_bucket5_backup = prop.r_zip_bucket5,
         prop.r_zip_bucket5 = as.character(
           as.numeric(prop.r_zip_bucket5_start) + 
             (as.numeric(prop.r_zip_bucket5_end) - as.numeric(prop.r_zip_bucket5_start))/2
         )) 
```

```{r g_perc_gop_hist_ready_ALL_VOTERS}
g_perc_gop_hist_ready_ALL_VOTERS <- df_perc_gop_hist_ready_ALL_VOTERS %>% 
  # Filter to just overall
  filter(party == "Overall (D + I + R)") %>% 
  # Plot
  ggplot(aes(x = prop.r_zip_bucket5,
             y = weight_measure_value/100,
             color = "Overall",
             fill = "Overall")) +
  geom_bar(stat = "identity") +
  geom_point(data = df_perc_gop_hist_ready_ALL_VOTERS %>%
               filter(party != "Overall (D + I + R)"),
             aes(color = party,
                 fill = party,
                 group = party),
             show.legend = FALSE) +
  geom_line(data = df_perc_gop_hist_ready_ALL_VOTERS %>%
               filter(party != "Overall (D + I + R)") %>% 
              mutate(party = fct_drop(party)),
             aes(color = party,
                 fill = party,
                 group = party),
             show.legend = FALSE) +
  # Theme + Structure
  theme_minimal() +
  theme(legend.position = "bottom",
        strip.text.x = element_text(size = 12)) +
  facet_wrap( ~ `Data Source`) +
  # Labels and Scales
  labs(x = "Share of GOP",
       y = "Share of Population") +
  scale_color_manual(name = NULL,
                     values = c("Democrats" = colors_with_independents[["Democrat"]],
                                "Republicans" = colors_with_independents[["Republican"]],
                                "Overall" = "darkgray")) + 
  scale_fill_manual(name = NULL,
                     values = c("Democrats" = colors_with_independents[["Democrat"]],
                                "Republicans" = colors_with_independents[["Republican"]],
                                "Overall" = "lightgray")) 

g_perc_gop_hist_ready_ALL_VOTERS
```

```{r SAVE_g_perc_gop_hist_ready_ALL_VOTERS}
ggsave(filename = "Figures/Figure A2.1.pdf",
       plot = g_perc_gop_hist_ready_ALL_VOTERS,
       width = 9, 
       height = 4, 
       dpi = 300)
```


